﻿using AutoMapper;
using MediatR;
using Management.API.Application.Controller.Management;
using Management.Domain;
using Management.Errorcode;
using Management.Errorcode.ErrorCode;
using Management.Infrastructure.Interfaces;
using Management.Infrastructure.Interfaces.BOMManagement;
using Management.Models.BOMBasicInformation;


namespace Management.API.Application.CommandHandler
{
    
    public class AddBasicInformationCommandHandler : IRequestHandler<AddBasicInformationCommand, ApiResult>
    {
        private readonly IBasicInformationRepository _iBasicInformationRepository;
        private readonly IMapper _mapper; 
        private readonly ILogger<AddBasicInformationCommandHandler> _logger;

        public AddBasicInformationCommandHandler(IBasicInformationRepository IBasicInformationRepository, IMapper mapper, ILogger<AddBasicInformationCommandHandler> logger)
        {
            _iBasicInformationRepository = IBasicInformationRepository;
            _mapper = mapper;
            _logger = logger;
        }

        public async Task<ApiResult> Handle(AddBasicInformationCommand request, CancellationToken cancellationToken)
        {

            try
            {
                // 将 DTO 映射为实体类
                var userEntity = _mapper.Map<BasicInformation>(request);

                // 调用仓储层添加BOM
                var addedUser = await _iBasicInformationRepository.InsertAsync(userEntity);

                // 返回成功结果
                return ApiResult.Success(StatusCodeEnum.Success);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "添加BOM信息失败");
                return ApiResult.Fail("添加BOM信息失败", StatusCodeEnum.InternalServerError);
            }
        }




    }
}
